package me.pplay.playtalk.activity.setting;

import me.pplay.playtalk.ApplicationTrans;
import me.pplay.playtalk.R;
import me.pplay.playtalk.activity.BaseActivity;
import me.pplay.playtalk.activity.ContactDetailsAboutActivity;
import me.pplay.playtalk.activity.PlayTalkActivity;
import me.pplay.playtalk.activity.SignUpActivity;
import me.pplay.playtalk.activity.TextInfoActivity;
import me.pplay.playtalk.data.Globals;
import me.pplay.playtalk.net.SkyidOperation;
import me.pplay.playtalk.protocol.DataCallBack;
import me.pplay.playtalk.protocol.TpChat;
import me.pplay.playtalk.protocol.bean.RspData;
import me.pplay.playtalk.util.LogManager;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;

public class SettingActivity extends BaseActivity {
	LinearLayout set_myprofile;
	LinearLayout set_changepassword;
	LinearLayout set_privacycontrol;
	LinearLayout set_autotext;
	LinearLayout set_storesetting;
	LinearLayout set_instructions;
	LinearLayout set_feedback;
	LinearLayout set_about;
	Button btn_set_sign;
	private ProgressDialog progressDialog=null;
	Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			if (progressDialog != null)
				progressDialog.dismiss();
			LogManager.LogShow("SettingActivity", "handleMessage msg.what = " + msg.what);
			switch (msg.what) {
			case TpChat.CHAT_GET_PRIVATE_TIMEOUT :
				showToast("Time out");
				break;
			case TpChat.CHAT_GET_PRIVATE_ERR:
				showToast("Err");
				break;
			case TpChat.CHAT_GET_PRIVATE_OK:
				Intent intent = new Intent(SettingActivity.this,
						SettingPrivacyControlActivity.class);  
				startActivity(intent);
			default:
				break;
			}
		}

	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_setting);
		super.SetOnBackClickListener((ImageButton)findViewById(R.id.btn_back));
		set_myprofile = (LinearLayout)findViewById(R.id.set_myprofile);
		set_changepassword = (LinearLayout)findViewById(R.id.set_changepassword);
		set_privacycontrol = (LinearLayout)findViewById(R.id.set_privacycontrol);
		set_autotext = (LinearLayout)findViewById(R.id.set_autotext);
		//set_storesetting = (LinearLayout)findViewById(R.id.set_storesetting);
		set_instructions = (LinearLayout)findViewById(R.id.set_instructions);
		set_feedback = (LinearLayout)findViewById(R.id.set_feedback);
		set_about = (LinearLayout)findViewById(R.id.set_about);
		btn_set_sign = (Button)findViewById(R.id.btn_set_sign);
		if(SkyidOperation.getUserName(getApplicationContext()).equals(""))
			btn_set_sign.setText(getString(R.string.signup));
		clickEvents();
		
	}

	private void clickEvents() {
		set_myprofile.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();
				intent.putExtra("skyid", Globals.skyid);
				intent.setClass(SettingActivity.this, ContactDetailsAboutActivity.class);
				startActivity(intent);
			}
		});
		set_changepassword.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(SettingActivity.this,
						SettingChPwdActivity.class);  
				startActivity(intent);
			}
		});
		set_privacycontrol.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				getPrivate();
			}
		});
		set_autotext.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(SettingActivity.this,
						SettingAutoTextActivity.class);  
				startActivity(intent);
			}
		});
		/*
		set_storesetting.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(SettingActivity.this,
						SettingStoreSettingActivity.class);  
				startActivity(intent);
			}
		});
		*/
		set_instructions.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(SettingActivity.this,
						TextInfoActivity.class);
				Bundle bundle=new Bundle();
				bundle.putString("title", getString(R.string.set_instructions));
				bundle.putString("info", getString(R.string.set_instructionsdes));
				intent.putExtras(bundle);       
				startActivity(intent);
			}
		});
		set_feedback.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(SettingActivity.this,
						TextInfoActivity.class);
				Bundle bundle=new Bundle();
				bundle.putString("title", getString(R.string.set_feedback));
				bundle.putString("info", getString(R.string.set_feedbackdes));
				intent.putExtras(bundle);       
				startActivity(intent);
			}
		});
		set_about.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(SettingActivity.this,
						TextInfoActivity.class);
				Bundle bundle=new Bundle();
				bundle.putString("title", getString(R.string.set_about));
				bundle.putString("info", getString(R.string.set_aboutdes));
				intent.putExtras(bundle);       
				startActivity(intent);
			}
		});
		btn_set_sign.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(SkyidOperation.getUserName(getApplicationContext()).equals("")){
					//如果是ping用户的是sign up 点击到sign up 界面
					Intent intent = new Intent(SettingActivity.this, SignUpActivity.class);
					startActivity(intent);
				}else{
					//如果有帐号的是sign out 点击退到let'chating 界面
					ApplicationTrans appState = (ApplicationTrans) getApplicationContext();
					
					Intent intent = new Intent(SettingActivity.this, PlayTalkActivity.class);
					Bundle bundle = new Bundle();
					bundle.putString("from", "settingActivity");
					intent.putExtras(bundle);
					appState.closeAllActivity();
					appState.tp_exit();
					startActivity(intent);
				}
			}
		});
		 
	} 
//	private void showToast(String str) {
//		Toast.makeText(SettingActivity.this, str, Toast.LENGTH_SHORT).show();
//	}
	private void getPrivate() 
	{
		ApplicationTrans appState = (ApplicationTrans) getApplicationContext();
		TpChat tpChat = appState.getTpChat();
		if(tpChat.tp_chat_get_private_data()!=null){
			Message msg = new Message(); 
			msg.what = TpChat.CHAT_GET_PRIVATE_OK; 
			handler.sendMessage(msg);
			return;
		}
		progressDialog = ProgressDialog.show(SettingActivity.this, getString(R.string.loading), getString(R.string.waittng), true, false); 
		progressDialog.show();
		tpChat.tp_chat_get_private(Globals.skyid, new DataCallBack() {
			
			@Override
			public void Callback(int result, RspData resultdata, Object obj) {
				Message msg = new Message(); 
				msg.what = result; 
				msg.obj = resultdata;
				switch (result) {
				case TpChat.CHAT_GET_PRIVATE_TIMEOUT :
					break;
				case TpChat.CHAT_GET_PRIVATE_ERR:
					break;
				case TpChat.CHAT_GET_PRIVATE_OK:
					msg.obj = obj;
					break;
				}
				handler.sendMessage(msg);
			}
		});
	}
}
